package com.yj.suncity.zxing;

/**
 * GenerateActivity.java [V 1..0.0]
 * classes : com.zhangyx.TwoDimensionalCode.util.ZXing.generate.GenerateActivity
 * zhangyx Create at 2014-12-22 下午4:38:21
 */

import java.util.Hashtable;

import com.yj.suncity.R;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 生成二维码 com.zhangyx.TwoDimensionalCode.util.ZXing.generate.GenerateActivity
 * 
 * @author Admin-zhangyx
 * 
 *         create at 2014-12-22 下午4:38:21
 */
public class GenerateActivity extends Activity {

	private String id;
	private int flag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_generate);
		id = getIntent().getStringExtra("id");
		flag=getIntent().getIntExtra("flag", -1);
		if (flag==1) {
			((TextView)findViewById(R.id.txt_title_name)).setText("确认抵购二维码");
		} else if (flag==2){
			((TextView)findViewById(R.id.txt_title_name)).setText("确认兑换二维码");
		}
	
//		String contents = "BEGIN:VCARD\nVERSION:3.0\n" + "N:" + name + "\nORG:"
//				+ company + "\nTITLE:" + position + "\nNOTE:" + note + "\nTEL:"
//				+ phone + "\nADR:" + add + "\nURL:" + web1 + "\nEMAIL:" + email
//				+ "\nEND:VCARD";
	
		try {
			Bitmap bm = qr_code(id, BarcodeFormat.QR_CODE);

			ImageView img = (ImageView) findViewById(R.id.img_only);

			img.setImageBitmap(bm);
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		

	}
	
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		case R.id.btn_ok:
			
			break;
		default:
			break;
		}
	}

	public Bitmap qr_code(String string, BarcodeFormat format)
			throws WriterException {
		MultiFormatWriter writer = new MultiFormatWriter();
		Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>();
		hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix matrix = writer.encode(string, format, 400, 400, hst);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int[] pixels = new int[width * height];
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = 0xff000000;
				}

			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		// 通过像素数组生成bitmap,具体参考api
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}

}
